package ch.fhnw.qualiscan.application.models;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.core.Model;
import ch.fhnw.qualiscan.core.db.pojos.Answer;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.Language;
import ch.fhnw.qualiscan.core.db.pojos.Question;
import ch.fhnw.qualiscan.core.db.pojos.QuestionText;
import ch.fhnw.qualiscan.core.db.pojos.SurveyUserRole;

/**
 * 
 * @author Kevin Mettenberger
 *
 */

public class QuestionModel extends Model{
	
	/**
	 * Adds a new question to a specified domain.
	 * 
	 * @param domain The questions domain.
	 * @return Returns the saved question object.
	 */
	public Question addQuestion(Domain domain) {

		Domain d = (Domain)hibernateSession.getSession().get(Domain.class, domain.getId());
		Question q = new Question();
		hibernateSession.getSession().save(q);
		d.addQuestion(q);
	
		hibernateSession.getSession().flush();
		hibernateSession.closeSession();
		return q;
		
	}
	
	/**
	 * Adds a a question text in a specified language to a question.
	 * 
	 * @param question The question the text is being added to.
	 * @param language The language of the question text.
	 * @param text The text of the question.
	 * @return Returns the saved question text.
	 */
	public QuestionText addQuestionText(Question question, Language language, String text) {
		
		QuestionText qt = new QuestionText();
		qt.setLanguage(language);
		qt.setText(text);
		
		hibernateSession.getSession().save(qt);
		
		Question q = (Question)hibernateSession.getSession().load(Question.class, question.getId());
		q.addQuestiontext(qt);
		
		hibernateSession.getSession().flush();
		hibernateSession.closeSession();
		
		return qt;
	}
	/**
	 * Gets the question text for a specified language and question.
	 * 
	 * @param language the language
	 * @param question the question
	 * @return a QuestionText object
	 */
	public QuestionText getQuestionText(Language language, Question question) {
		QuestionText qt = (QuestionText) hibernateSession.getSession().createQuery("from QuestionText as qt where question = :question and language = :language")
		.setParameter("question", question)
		.setParameter("language", language)
		.uniqueResult();
		
		if (qt == null) {
			Language l = new Language();
			l.setId(Config.ENGLISH_ID);
			
			qt = (QuestionText) hibernateSession.getSession().createQuery("from QuestionText as qt where question = :question and language = :language")
			.setParameter("question", question)
			.setParameter("language", l)
			.uniqueResult();
		}
		
		return qt;
	}
	
	/**
	 * 
	 * @param SurveyUserRole SurveyUserRole object
	 * @param question the answered question
	 * @return returns an answer object if there's already one, else null.
	 */
	public Answer getAnswer(SurveyUserRole sur, Question question) {
		Answer a = (Answer)hibernateSession.getSession().createQuery("from Answer as a where sur  = :sur and question = :question")
		.setParameter("sur", sur)
		.setParameter("question", question)
		.uniqueResult();
		
		return a;
	}
	
	/**
	 * Saves a answer to the db.
	 * @param answer the answer to be saved.
	 */
	public void saveAnswer(Answer answer, Question question) {
		hibernateSession.getSession().save(answer);
		Question q = (Question) hibernateSession.getSession().get(Question.class,question.getId());
		q.addAnswer(answer);
		
		hibernateSession.getSession().flush();
		hibernateSession.closeSession();
	}
	
	/**
	 * Updates a answer in the db.
	 * @param answer the answer to be updated
	 */
	public void updateAnswer(Answer answer) {
		hibernateSession.getSession().update(answer);
		hibernateSession.closeSession();
	}
	
	/**
	 * Gets a question object for a specified id 
	 * @param questionId the id of the requested question
	 * @return question object
	 */
	public Question getQuestionById(int questionId) {
		return (Question) hibernateSession.getSession().get(Question.class, questionId);
	}
	

	


}
